package com.ucode.wap.hrm.service;

import cn.hutool.core.util.ObjUtil;
import com.ucode.hrm.service.HrmEmployeeService;
import com.ucode.model.hrm.HrmEmployee;
import com.ucode.model.hrm.enums.HrmEmploymentTypeEnum;
import com.ucode.model.hrm.enums.HrmSysResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: liliang
 * @Date: 2025/6/15 9:21
 * @Version: 1.0
 */
@Service
public class ApiHrmEmployeeService {

    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;

    @Resource
    private HrmEmployeeService employeeService;

    /**
     * 验证当前用户是否绑定员工，并获取员工ID
     * @return
     */
    public Long verifyCurrentUserBindAndGetEmployeeId() {
        HrmEmployee bean = verifyCurrentUserBindAndGetEmployee();
        return bean.getId();
    }

    /**
     * 验证当前用户是否绑定员工
     * @return
     */
    public HrmEmployee verifyCurrentUserBindAndGetEmployee() {
        HrmEmployee bean = employeeService.selectBindStatusInfo(currentLoginUserHandler.getCurrentUserId());
        verifyEmployeeStatus( bean);

        return bean;
    }

    /**
     * 验证员工状态
     * @param bean
     */
    public void verifyEmployeeStatus(HrmEmployee bean){
        if(ObjUtil.isNull(bean) || bean.getDeltag()) {
            throw new UcodeServiceException(HrmSysResultCodeEnum.EMPLOYEE_IDENTITY_ENTRY_NOT_EXISTS);
        }

        if(ObjUtil.equals(bean.getEmploymentType(), HrmEmploymentTypeEnum.LEFT.getCode())) {
            throw new UcodeServiceException(HrmSysResultCodeEnum.EMPLOYEE_HAVE_QUITTED_ERROR);
        }

        if(ObjUtil.equals(bean.getEmploymentType(), HrmEmploymentTypeEnum.APPLY.getCode())){
            throw new UcodeServiceException(HrmSysResultCodeEnum.EMPLOYEE_IDENTITY_ENTRY_NOT_EXISTS);
        }

        if(ObjUtil.isNotNull(bean.getMemberId()) && !ObjUtil.equals(bean.getMemberId(),  0L) &&
                !ObjUtil.equals(bean.getMemberId(),  currentLoginUserHandler.getCurrentUserId())){
            throw new UcodeServiceException(HrmSysResultCodeEnum.EMPLOYEE_BIND_OTHER_ERROR);
        }

    }

}
